using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonUtils;

namespace ShellAnywhere.Model
{
	public class Action : Param, IAction
	{
		#region ctors
		public Action(IVerb verb, ITarget target, params IParam[] paramz)
		{
			Verb = verb;
			Target = target;
			Params = paramz;
		}
		#endregion ctors

		#region properties
		public string Description
		{
			get
			{
				string description = "";
				if (Verb != null && !string.IsNullOrEmpty(Verb.Description))
				{
					description += "[" +  Verb.Description + "] ";
					if (Target != null && !string.IsNullOrEmpty(Target.Description))
						description += " \"" + Target.Description + "\"";
				}				
				return description;
			}
		}
		public IVerb Verb { get; set; }
		public ITarget Target { get; set; }
		public IParam[] Params { get; set; }		

		public override string Name 
		{ 
			get 
			{
				string verb = (Verb != null) ? Verb.Name : "";
				string target = (Target != null) ? Target.Name : "";
				string paramz = Common.ObjectArrayToString(Params, null, " ", null);
				string space1 = (verb.Length > 0 && target.Length > 0) ? " " : "";
				string space2 = (target.Length > 0 && paramz.Length > 0) ? " " : "";
				return verb + space1 + target + space2 + paramz;
			}
		}
		private List<IParam> ParamList
		{
			get
			{
				List<IParam> paramList = new List<IParam>();
				if (Verb != null)
					paramList.Add(Verb);
				if (Target != null)
					paramList.Add(Target);
				if (Params != null)
					paramList.AddRange(Params);
				return paramList;
			}
		}
		#endregion properties

		#region methods
		public override char CharAt(int dot)
		{
			return base.CharAt(dot);
		}
		public override string Matched(int dot)
		{
			return base.Matched(dot);
		}
		public override string Unmatched(int dot)
		{
			return base.Unmatched(dot);
		}
		public override bool IsMatched(int dot)
		{
			return base.IsMatched(dot);
		}
		public override bool IsLastDot(int dot)
		{
			return base.IsLastDot(dot);
		}
		public bool Execute()
		{
			return Verb.Execute(Target, Params);
		}
		private bool IsCompound()
		{
			return Verb != null && Target != null && Verb.Name.Length > 0 && Target.Name.Length > 0;
		}
		#endregion methods

		#region fields
		#endregion fields
	}
}
